﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;
using Dict;

namespace UI
{
    public class UIYggdrasilEvent : MonoBehaviour
    {
        /// <summary>
        /// 关闭
        /// </summary>
        public void OnClickExit()
        {
            GetComponent<UIYggdrasil>().Close();
        }
        

        /// <summary>
        /// 点亮等级
        /// </summary>
        public void OnClickLightUp()
        {
            if (canClick)
            {
                currline = 0f;
                var yggdrasil = GetComponent<UIYggdrasil>();

                if (!CommonMethod.GetModuleOpen("worldTreeUnlock"))
                {
                    string str = TableTextBlo.GetText("x_level_open");
                    var module = DictModuleBlo.GetTableModule("worldTreeUnlock");

                    str = string.Format(str, module.openLev) + TableTextBlo.GetText(module.modelName);

                    BoxManager.CreatePopupTextBox(str);
                }
                else if (yggdrasil.totalSeedHas < yggdrasil.nextSeedUsed)
                {
                    string str = TableTextBlo.GetText("not_enough");
                    string str0 = TableTextBlo.GetText("world_seed");
                    BoxManager.CreatePopupTextBox(str0 + str);
                }
                else if (!yggdrasil.maxLevel)
                {
                    CSLightenWorldSeed light = new CSLightenWorldSeed();
                    light.id = StaticData.playerData.CurrentWorldSeedLevel;
                    

                    NetAPI.SendHttp(OpDefine.CSLightenWorldSeed, light, (o, ok) =>
                    {
                        if (ok)
                        {
                            GetComponent<UIYggdrasil>().PlayEffect();

                            SCLightenWorldSeed seed = (SCLightenWorldSeed)o.kBody;
                            StaticData.playerData.Delete(seed.cost);
                            StaticData.playerData.CurrentWorldSeedLevel += 1;
                            if (seed.get != null)
                            {
                                if (seed.get.equips.Count > 0 || seed.get.simps.Count > 0 ||
                                 seed.get.treas.Count > 0 || seed.get.wars.Count > 0)
                                {
                                    //CommonMethod.ShowGetObjs(seed.get);
                                    StartCoroutine(ShowGet(seed));
                                    StaticData.playerData.AddResultObj(seed.get);
                                }
                            }
                            if (seed.newWarrior != null)
                            {
                                StaticData.playerData.UpdateHero(seed.newWarrior);
                                string guid = seed.newWarrior.guid;
                                StaticData.playerData.DeleteHero(guid);

                                List<Warrior> warriors = new List<Warrior>();
                                warriors.Add(seed.newWarrior);

                                StaticData.playerData.AddHero(warriors);


                                foreach(var fHero in StaticData.playerData.InstPlayerData.BattleCamps)
                                {
                                    if(fHero.Value.hero!=null && fHero.Value.hero.guid == guid)
                                    {
                                        fHero.Value.MainHeroAdvance(StaticData.playerData.GetUIHero(guid));
                                        StaticData.playerData.InstPlayerData.ResetAllFormationAttribute();
                                        break;
                                    }
                                }

                            }
                            GetComponent<UIYggdrasil>().Reflush();
                            if (!CommonMethod.GetModuleOpen("worldTreeUnlock") || yggdrasil.totalSeedHas < yggdrasil.nextSeedUsed || yggdrasil.maxLevel)
                                StaticData.redTips.SetPointState(RedPointTip.Red.worldTree, 0);

                            if (yggdrasil.onWorldSeedUpdate != null)
                            {
                                yggdrasil.onWorldSeedUpdate(yggdrasil.totalSeedHas, yggdrasil.nextSeedUsed);
                            }
                        }
                    });
                }
                else
                {
                    BoxManager.CreatePopupTextBox(TableTextBlo.GetText("worldtree_level_up_max"));
                }
            }
        }

        public void OnClick0()
        {
            GetComponent<UIYggdrasil>().ShowPopupText(0);
        }
        public void OnClick1()
        {
            GetComponent<UIYggdrasil>().ShowPopupText(1);
        }
        public void OnClick2()
        {
            GetComponent<UIYggdrasil>().ShowPopupText(2);
        }
        public void OnClick3()
        {
            GetComponent<UIYggdrasil>().ShowPopupText(3);
        }

        public void OnClick4() { }

        public void OnOpenInnerPanel()
        {
            GetComponent<UIYggdrasil>().OpenInnerPanel();
        }

        public void OnCloseInnerPanel()
        {
            GetComponent<UIYggdrasil>().ClosedInnerPanel();
        }

        public float timeline = 2.5f;
        float currline = 0f;
        bool canClick = true;
        void Update()
        {
            //return;
            currline += Time.deltaTime;
            if(currline > timeline)
            {
                canClick = true;
                //GetComponent<UIYggdrasil>().levelupBtn.state = UIButtonColor.State.Normal;
            }
            else
            {
                canClick = false;
                //GetComponent<UIYggdrasil>().levelupBtn.state = UIButtonColor.State.Disabled;
            }
        }

        IEnumerator ShowGet(SCLightenWorldSeed seed)
        {
            yield return new WaitForSeconds(1.5f);

            CommonMethod.ShowPopupUpTextObj(seed.get);
        }
    }
}